package com.dc.web.controller;


import com.dc.constant.MessageConstant;
import com.dc.dao.CalibrationPaintShipDao;
import com.dc.dao.ShipDao;
import com.dc.entity.CalibrationPaintHouseEntity;
import com.dc.entity.CalibrationPaintShipEntity;
import com.dc.entity.ShipEntity;
import com.dc.exception.ServiceException;
import com.dc.model.AjaxResult;
import com.dc.model.PageListBody;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@Api(tags = "4-涂装喷漆-0-舱室及船壳喷漆")
@RestController
@RequestMapping("/api/calibration_paint_ship")
@Slf4j
public class CalibrationPaintShipController {

    @Resource
    private CalibrationPaintShipDao calibrationPaintShipDao;

    @Resource
    private ShipDao shipDao;

    @GetMapping("/list")
    public AjaxResult list(PageListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        Page<CalibrationPaintShipEntity> all = calibrationPaintShipDao.findAll(pageRequest);

        all.getContent().forEach(t-> {
            t.setShipId(t.getShip().getId());
            t.setShipName(t.getShip().getName());
        });

        return AjaxResult.success(all);
    }

    @GetMapping("/{id}")
    public AjaxResult show(@PathVariable("id") Long id) {
        Optional<CalibrationPaintShipEntity> optional = calibrationPaintShipDao.findById(id);
        CalibrationPaintShipEntity entity = null;
        if (optional.isPresent()) {
            entity = optional.get();
            entity.setShipId(entity.getShip().getId());
            entity.setShipName(entity.getShip().getName());
            return AjaxResult.success(entity);
        } else {
            return AjaxResult.error("数据不存在! id = " + id);
        }
    }

    @PostMapping()
    public AjaxResult add(@RequestBody CalibrationPaintShipEntity entity) {
        entity.setId(null);
        Optional<ShipEntity> optionalShip = shipDao.findById(entity.getShipId());
        if(!optionalShip.isPresent()) {
            throw new ServiceException(MessageConstant.ERR_SHIP_MISSING);
        }
        ShipEntity ship = optionalShip.get();

        entity.setShip(ship);
        // 动态计算
        calc(entity);

        CalibrationPaintShipEntity save = calibrationPaintShipDao.save(entity);
        save.setShipName(ship.getName());
        return AjaxResult.success(save);
    }

    @PutMapping("/{id}")
    public AjaxResult update(@PathVariable("id") Long id, @RequestBody CalibrationPaintShipEntity entity) {
        entity.setId(id);
        ShipEntity ship = shipDao.findById(entity.getShipId()).get();
        entity.setShip(ship);
        // 动态计算
        calc(entity);

        CalibrationPaintShipEntity save = calibrationPaintShipDao.save(entity);
        save.setShipId(ship.getId());
        save.setShipName(ship.getName());
        return AjaxResult.success(save);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        calibrationPaintShipDao.deleteById(id);
        return AjaxResult.success();
    }

    private CalibrationPaintShipEntity calc(CalibrationPaintShipEntity entity) {
        try {
            entity.air = entity.endAir - entity.startAir;
        } catch (Exception e) {
            entity.air = 0.;
        }

        try {
            entity.airByUnit = entity.air / entity.area;
        } catch (Exception e) {
            entity.airByUnit = 0.;
        }

        return entity;
    }
}
